var express = require('express');
var router = express.Router();
var User =require('../database/user.model');
var Top =require('../database/top.model');

/* GET index page. */
router.get('/', function(req, res,next) {
  res.render('index', { title: 'Express' });    // 到达此路径则渲染index文件，并传出title值供 index.html使用
});


///提交成绩，同时获得排名
router.post('/register',InputToRankList);
router.get('/rankList',GetRankList);
router.post('/postrank',PutRankScore);



function InputToRankList (req,res) {
    var uname = req.body.uname;
    var uscore = req.body.score;
    var ucreateTime =Date.now();

    User.count({score:{$gt:uscore}},function(error,comment){
        if(error){
            console.log(error);
        }else
        {
            var urank =comment+1;


            try{
                //创建该次成绩
                User.create({
                    name: uname,
                    score: uscore,
                    createTime:ucreateTime
                },function(err,doc){
                    if (err) {
                        console.log(err);
                        res.sendStatus(500);
                    } else {

                        var response ={};
                        response.rank =urank;
                        //是否进入前10

                        if(urank<10){
                            Top.find({}).sort({'score':-1}).exec(function(err,comment){
                                if(err){
                                    console.log(err);
                                }else{
                                    //console.log("进入 10 的数量"+comment.length);
                                    if(comment.length>9){
                                        //console.log("执行删除"+comment[9]);
                                        Top.findByIdAndRemove(comment[9]._id,function (err, comme) {
                                            if(err){
                                                console.log(err);
                                            }
                                        });
                                    }


                                    var top =new Top({
                                        rank:urank,
                                        name:doc.name,
                                        score:doc.score,
                                        u_id:doc.objectId,
                                    });
                                    top.save(function (err,com) {
                                        if (err) {
                                            console.log("Error:" + err);
                                        }
                                        else {
                                            Top.find({}).sort({'score':-1}).exec(function(err,comment){

                                                response.topList=comment;

                                                res.send(response);
                                            });
                                        }

                                    });
                                }


                            });
                        }else
                        {

                            Top.find({}).sort({'score':-1}).exec(function(err,comment){
                                response.topList=comment;

                                res.send(response);
                            });


                        }



                        //res.sendStatus(200);

                    }
                });
            }catch(e){
                console.log(e);
            }

        }
    });


}

function PutRankScore(req,res){
    var uname = req.body.uname;
    var uscore = req.body.score;
    var ucreateTime =Date.now();
    Top.find({}).sort({'score':-1}).exec(function(err,comment){
        if(err){
            console.log(err);
        }else{
            if(comment.length>9){
                //console.log("执行删除"+comment[9]);
                Top.findByIdAndRemove(comment[9]._id,function (err, comme) {
                    if(err){
                        console.log(err);
                    }
                });
            }

            var top =new Top({
                name:uname,
                score:uscore,
                createTime:ucreateTime,
            });
            top.save(function (err,com) {
                if (err) {
                    console.log("Error:" + err);
                }
                else {
                    Top.find({}).sort({'score':-1}).exec(function(err,comment){

                        var response ={};
                        response.topList=comment;

                        res.send(response);
                    });
                }

            });



        }
    });
}


function GetRankList(req,res){


    Top.find({}).sort({'score':-1})
	 .select({'name':1,'score':1})
	.exec(function(err,comment){

        var response ={};
        response.topList=comment;
        response.min =comment[comment.length-1];

        res.send(response);
    });

}

module.exports = router;
